﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;
using QLVMBAYDatacess;
using System.Drawing;

namespace QLVMBAY.Pages
{
    public partial class pRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnLuu.Visible = lblMK.Visible = lblTenDN.Visible = txtTenND.Visible = txtMK.Visible = Session[ClientComm.KEY_DANG_NHAP] == null;
                btnCancel.Visible = btnUpdate.Visible = Session[ClientComm.KEY_DANG_NHAP] != null;
                if (Session[ClientComm.KEY_DANG_NHAP] != null)
                {
                    var ngDung = (NguoiDung)Session[ClientComm.KEY_DANG_NHAP];
                    if (ngDung.LoaiND == LOAI_NGUOI_DUNG.USER)
                    {
                        txtTenND.Text = ngDung.MaNguoiDung;
                        txtMK.Text = ngDung.MatKhau;
                        var getHK = HanhKhachDA.Inst.GetByPrimaryKey(false, ngDung.MaHanhKhach);
                        txtCMND.Text = getHK.CMND;
                        txtDiaChi.Text = getHK.DiaChi;
                        txtEmail.Text = getHK.Email;
                        ddlGTinh.SelectedValue = getHK.GioiTinh.ToString();
                        txtHoChieu.Text = getHK.HoChieu;
                        txtHoTen.Text = getHK.TenHanhKhach;
                        txtNgaySinh.Text = getHK.NgaySinh;
                        txtSoDT.Text = getHK.DienThoai;


                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            var getTT = NguoiDungDA.Inst.GetByPrimaryKey(false, txtTenND.Text);
            if (getTT == null)
            {    
                
                var objHanhKhach = new HanhKhach();
                objHanhKhach.MaHanhKhach = "HK" + new Random().Next(0,999);
                objHanhKhach.CMND = txtCMND.Text;
                objHanhKhach.DiaChi = txtDiaChi.Text;
                objHanhKhach.DienThoai = txtSoDT.Text;
                objHanhKhach.Email = txtEmail.Text;
                objHanhKhach.GioiTinh = ddlGTinh.SelectedIndex;
                objHanhKhach.HoChieu = txtHoChieu.Text;
                objHanhKhach.NgayDangKy = DateTime.Now;
                objHanhKhach.NgaySinh = txtNgaySinh.Text;
                objHanhKhach.TenHanhKhach = txtHoTen.Text;

                var objNguoiDung = new NguoiDung();
                objNguoiDung.MaHanhKhach = objHanhKhach.MaHanhKhach;
                objNguoiDung.HoTenND = txtHoTen.Text;
                objNguoiDung.LoaiND = LOAI_NGUOI_DUNG.USER;
                objNguoiDung.MaNguoiDung = txtTenND.Text;
                objNguoiDung.MatKhau = txtMK.Text;
                var Res = HanhKhachDA.Inst.ThemHanhKhach(objHanhKhach, objNguoiDung);
                if (Res)
                {
                    Page.RegisterStartupScript("Err",
                                         "<script language='javascript'>alert('Đăng ký thành công!!!');</script>");
                    Response.Redirect("~/./Default.aspx");

                }
                else
                {

                    lblStatus.Text = "Có lỗi xãy ra,đăng ký chưa được thực hiện";
                    lblStatus.ForeColor = Color.Red;
                    lblStatus.Visible = true;

                }
            }
            else
            {
                Page.RegisterStartupScript("Err",
                                         "<script language='javascript'>alert('Tên đăng nhập đã tồn tại.');</script>");
                return;
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/./Default.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var objHanhKhach = new HanhKhach();
            objHanhKhach.MaHanhKhach = ((NguoiDung)Session[ClientComm.KEY_DANG_NHAP]).MaHanhKhach;
            objHanhKhach.CMND = txtCMND.Text;
            objHanhKhach.DiaChi = txtDiaChi.Text;
            objHanhKhach.DienThoai = txtSoDT.Text;
            objHanhKhach.Email = txtEmail.Text;
            objHanhKhach.GioiTinh = ddlGTinh.SelectedIndex;
            objHanhKhach.HoChieu = txtHoChieu.Text;
            objHanhKhach.NgayDangKy = DateTime.Now;
            objHanhKhach.NgaySinh = txtNgaySinh.Text;
            objHanhKhach.TenHanhKhach = txtHoTen.Text;
            var Res = HanhKhachDA.Inst.Update(objHanhKhach);
            if (Res)
            {
                Page.RegisterStartupScript("Err",
                                     "<script language='javascript'>alert('Thông tin lưu thành công!!!');</script>");
                Response.Redirect("~/./Default.aspx");

            }
            else
            {

                lblStatus.Text = "Có lỗi xãy ra, dữ liệu chưa được lưu.";
                lblStatus.ForeColor = Color.Red;
                lblStatus.Visible = true;

            }


        }        
    }
}